import json
from django.core.paginator import Paginator
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.shortcuts import render

# Create your views here.
from App.models import User
import App.foodNutrientSystemTest.chatbot_graph as chatbot

def login(request):
    if request.method == 'POST':
        userinfo = json.loads(request.body.decode('utf-8'))
        print(userinfo['username'], userinfo['password'])
        if userinfo['password'] == '':
            userinfo['password'] = None
        user = User.objects.filter(username=userinfo.get('username'), password=userinfo.get('password')).first()
        print(user)
        if user:
            # print(user.username, user.phone)
            uid = user.uid
            uname = user.username
            pwd = user.password
            phone = user.phone
            gender = user.gender
            age = user.age
            email = user.email
            status = user.status
            return HttpResponse(json.dumps({'ans': '1', 'uid': uid, 'username': uname, 'password': pwd,'phone': phone,'gender': gender,'age': age,'email': email, 'status': status}))
        return HttpResponse(json.dumps({'ans': '0'}))
    return HttpResponse()

def register(request):
    if request.method == "POST":
        userinfo = json.loads(request.body.decode('utf-8'))
        username = userinfo.get('username')
        password = userinfo.get('password')
        status = '1'
        user = User(username=username, password=password, status = status)
        user.save()
        print(username,password)
        # 返回ajax请求

        return JsonResponse({'code': '1'})
    return JsonResponse({'code': '0'})

def updateUser(request):
    if request.method == "POST":
        userinfo = json.loads(request.body.decode('utf-8'))
        uid = userinfo.get('uid')
        username = userinfo.get('username')
        password = userinfo.get('password')
        phone = userinfo.get('phone')
        gender = userinfo.get('gender')
        age = userinfo.get('age')
        email = userinfo.get('email')
        user = User.objects.get(pk=uid)
        print(user)
        user.username = username
        user.password = password
        user.phone = phone
        user.gender = gender
        user.age = age
        user.email = email
        user.save()
        return JsonResponse({'code': '1'})
    return JsonResponse({'code': '0'})

def intelligentAnswer(request):
    if request.method == "POST":
        questionInfo = json.loads(request.body.decode('utf-8'))
        question = questionInfo.get('question')
        handler = chatbot.ChatBotGraph()
        answer = handler.chat_main(question)
        return JsonResponse({'code': '1', 'answer': answer})
    return JsonResponse({'code': '0'})

def selectAll(request):
    user_list = []
    user_list = User.objects.all()
    d = []
    for user in user_list:
        d.append({'uid': user.uid, 'username': user.username, 'status': user.status, 'phone': user.phone, 'gender': user.gender, 'age': user.age, 'email': user.email})
    return JsonResponse(d, safe=False)

def deleteUser(request):
    if request.method == "POST":
        deleteInfo = json.loads(request.body.decode('utf-8'))
        deleteUid = deleteInfo.get('uid')
        try:
            user = User.objects.get(uid=deleteUid)
            print(user, type(user))
            if user:
                user.delete()
        except Exception as e:
            print(e)
            return JsonResponse({'code': '0'})
        return JsonResponse({'code': '1'})
    return JsonResponse({'code': '0'})

def insertUser(request):
    if request.method == "POST":
        userinfo = json.loads(request.body.decode('utf-8'))
        username = userinfo.get('username')
        password = userinfo.get('password')
        phone = userinfo.get('phone')
        gender = userinfo.get('gender')
        age = userinfo.get('age')
        email = userinfo.get('email')
        status = userinfo.get('status')
        user = User(username=username, password=password, age=age, gender=gender, phone=phone, email=email, status=status)
        user.save()
        return JsonResponse({'code': '1'})
    return JsonResponse({'code': '0'})

def updateUserStatus(request):
    if request.method == "POST":
        userinfo = json.loads(request.body.decode('utf-8'))
        uid = userinfo.get('uid')
        status = userinfo.get('status')
        user = User.objects.get(pk=uid)
        user.status = status
        user.save()
        return JsonResponse({'code': '1'})
    return JsonResponse({'code': '0'})



